package common.utils{
 
 import flash.external.ExternalInterface;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.net.navigateToURL;
 import flash.system.System;
 
 import mx.controls.Alert;
 
  public class WebUtil {
  	
  public static function OpenWindow(url:String, window:String="_blank", features:String=""):void
  {
     
   var WINDOW_OPEN_FUNCTION:String = "window.open";
   
   var myURL:URLRequest = new URLRequest(url);   
   var browserName:String = getBrowserName();
   
   try
   {
	   if(browserName == "Firefox"){
	    ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
	   }
	   //If IE, 
	   else if(browserName == "IE"){
	    ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
	    //ExternalInterface.call("function setWMWindow() {window.open(&apos;" + url + "&apos;);}");
	   }
	   //If Safari 
	   else if(browserName == "Safari"){     
	    navigateToURL(myURL, window);
	   }
	   //If Opera 
	   else if(browserName == "Opera"){ 
	    navigateToURL(myURL, window); 
	   } else {
	    navigateToURL(myURL, window);
	   }
   }
   catch(error:Error)
   {
	   System.setClipboard(url);
	   Alert.show("地址已成功复制，请粘贴到浏览器地址栏查看:)");   	
   }
   
   /*Alternate methodology...
      var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
   if(popSuccess == false){
    navigateToURL(myURL, window);
   }*/
    
  }
  
  private static function getBrowserName():String{
   var browser:String;
   
   if (!ExternalInterface.available)
   {
   	return "";
   }
   
   //Uses external interface to reach out to browser and grab browser useragent info.
   try
   {
   	var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
   }
   catch(error:Error)
   {
   	trace("ExternalInterface.call error:"+error.message);
   	browserAgent = "";
   }
    //  Debug.text += "Browser Info: [" + browserAgent + "]";
   
   //Determines brand of browser using a find index. If not found indexOf returns (-1).
   if(browserAgent != null && browserAgent.indexOf("Firefox") >= 0) {
    browser = "Firefox";
   } 
   else if(browserAgent != null && browserAgent.indexOf("Safari") >= 0){
    browser = "Safari";
   }    
   else if(browserAgent != null && browserAgent.indexOf("MSIE") >= 0){
    browser = "IE";
   }   
   else if(browserAgent != null && browserAgent.indexOf("Opera") >= 0){
    browser = "Opera";
   }
   else {
    browser = "";
   }
   return browser;
  }
  
   	public static function requestUrl(url:String):void
 	{
        var myRequest:URLRequest = new URLRequest(url);
		var myURLLoader:URLLoader = new URLLoader();
		
		try {
            myURLLoader.load(myRequest);
        } catch (error:Error) {
            trace("requestUrl failed: "+url);
        } 		
 	}
 }
} 

